$(function () {

  var page = 1;
  var pagesize = 10;
  var totalPage = 0;

  GetProduct();

  /* 商品的列表数据获取并展示在页面中 */
  function GetProduct() {
    $.ajax({
      type: 'get',
      url: '/product/queryProductDetailList',
      data: {
        page: page,
        pageSize: pagesize
      },
      success: function (res) {
        console.log(res);
        totalPage = Math.ceil(res.total / pagesize);
        $('#productBox').html(template('productTpl',
          res
        ));
      }
    });
  }

  $('#prevBtn').on('click', function () {
    page--;
    if (page < 1) {
      page = 1;
      alert('已经是第一页了');
      return;
    }
    GetProduct();
  });

  $('#nextBtn').on('click', function () {
    page++;
    if (paeg > totalPage) {
      page = totalPage;
      alert('已经是最后一页了');
      return;
    }
    GetProduct();
  })

  $.ajax({
    url: '',
    type: 'get',
    data: {
      page: 1,
      pageSize: 100
    },
    succss: function (res) {
      console.log(res);

      $('#brandBox').html(template('brandTpl', {
        data: res
      }))
    }
  })

  var pic = [];

  $('#fileUpload').fileupload({
    dataType: 'json',
    done: function (e, data) {
      pic.push(data._response.result);
      $('#imgBox').html(template('imgsTpl', {
        data: pic
      }))
    }
  });

  $('#addProduct').on('click', function () {
    var brandOptions = $('#brandOptions').val();
    var productName = $('#productName').val();
    var productDescription = $("#productDescription").val();
    var productNum = $("#productNum").val();
    var productSize = $("#productSize").val();
    var productOriginPrice = $("#productOriginPrice").val();
    var productNowPrice = $("#productNowPrice").val();

    if (brandOptions == -1) {
      alert('请选择品牌');
      return;
    }

    $.ajax({
      type: 'post',
      url: '',
      data: {
        proName: productName,
        oldPrice: productOriginPrice,
        price: productNowPrice,
        proDesc: productDescription,
        size: productSize,
        statu: 1,
        num: productNum,
        brandId: brandOptions,
        pic: pic
      },
      success: function (res) {
        if (res.success) {
          location.reload();
        } else {
          alert('商品添加失败');
        }
      }
    })

  })

})